<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018\7\17 0017
 * Time: 11:29
 */
namespace model\admin;
use model;
class customerModel extends model{

    public static function search( $filter = [] ){

        $sql = "SELECT c.*,cg.name as group_name FROM sh_customer c left join sh_customer_group cg on c.customer_group_id=cg.id ";

        $where = '';
        if( !empty($filter['name']) ){
            $where .= " and c.`name` LIKE '%" . $filter['name'] . "%' ";
        }
        if( !empty($filter['telephone']) ){
            $where .= " and c.`telephone` LIKE '%" . $filter['telephone'] . "%' ";
        }
        if( !empty($filter['customer_group_id']) ){
            $where .= " and c.`customer_group_id` =" . $filter['customer_group_id'];
        }

        if( !empty( $where ) ){
            $where = trim( $where , ' and');
            $sql .= ' where ' . $where;
        }


        $query = self::$db->query( $sql );

        return $query->rows;


    }

    public static function info( $customer_id ){

        $sql = 'SELECT 
                  c.*,
                  cg.name AS group_name 
                FROM 
                  sh_customer c 
                  LEFT JOIN sh_customer_group cg 
                    ON c.`customer_group_id` = cg.`id` 
                WHERE c.id = ' . $customer_id;

        $query = self::$db->query( $sql );

        return $query->row;
    }

    public static function edit( $customer_id, $data ){

        $sql = "UPDATE 
                  sh_customer 
                SET
                  customer_group_id = '" . $data['customer_group_id']
            ."',`name` = '" . $data['name']
            ."',telephone = '" . $data['telephone']
            ."',`status` = '" . $data['status']
            ."'WHERE id = " . $customer_id;

        $query = self::$db->query( $sql );

        //todo 修改用户地址

        self::remove_customer_address( $customer_id );
        if( !empty( $data['address_list'] ) ){
            self::add_customer_address( $customer_id, $data['address_list'] );
        }

        return $query->num_rows;

    }

    public static function remove_customer_address( $customer_id ){

        $sql = 'DELETE FROM sh_customer_address WHERE customer_id=' . $customer_id;
        self::$db->query( $sql );

    }

    public static function add_customer_address( $customer_id, $address_list ){

        $sql = 'INSERT INTO sh_customer_address (
                  `customer_id`,
                  `name`,
                  `telephone`,
                  `province_id`,
                  `province_name`,
                  `city_id`,
                  `city_name`,
                  `area_id`,
                  `area_name`,
                  `detail_address`,
                  `is_default`
                ) 
                VALUES ';

        $values = [];
        foreach( $address_list as $addr ){
            $values[] = "("
                . "'"   . $customer_id
                . "','" . $addr['name']
                . "','" . $addr['telephone']
                . "','" . $addr['province_id']
                . "','" . $addr['province_name']
                . "','" . $addr['city_id']
                . "','" . $addr['city_name']
                . "','" . $addr['area_id']
                . "','" . $addr['area_name']
                . "','" . $addr['detail_address']
                . "','" . $addr['is_default']
                ."')";
        }

        if( !empty( $values ) ){

            $sql .= implode( ',', $values );
            $query = self::$db->query( $sql );
            return $query->row;

        }

        return 0;
    }

    public static function get_address_list( $customer_id ){

        $sql = "SELECT * FROM sh_customer_address WHERE customer_id=" . $customer_id ;
        $query = self::$db->query( $sql );
        return $query->rows;
    }

}
